<template>
  <dragConent>
    <div class="clip-audio">
      <div class="clip-audio-content">
        <div class="clip-audio-title">录音</div>
        <div class="clip-audio-wave"></div>
        <div class="clip-audio-frequency"></div>
        <div class="clip-audio-hr"></div>
        <div class="clip-audio-btn" @mousedown.stop>
          <div v-if="recordingStatus === 0" class="clip-audio-btn-item" @click="startRecord">
            开始录音
          </div>
          <div
            v-else-if="recordingStatus === 1"
            class="clip-audio-btn-item"
            @click="pauseRecording"
          >
            暂停录音
          </div>
          <div
            v-else-if="recordingStatus === 2"
            class="clip-audio-btn-item"
            @click="continueRecording"
          >
            继续录音
          </div>
          <div class="clip-audio-btn-item" @click="pauseRecord">停止录音</div>
          <div class="clip-audio-btn-item" @click="returnToPreviousLevel">完成</div>
        </div>
      </div>
    </div>
  </dragConent>
</template>

<script lang="ts" setup>
import audioRecorder from '@/utils/recordingSettings' // 引入录音模块
import hooks from '@/hooks/index'
const router = useRouter()
const selectedDevice = ref('')
const dragConent = defineAsyncComponent(() => import('@/components/Dragareacomponent.vue'))

const recordingStatus = ref(0)
const startRecord = async () => {
  //开始录音
  recordingStatus.value = 1
  window.electron.ipcRenderer.invoke('start-record', selectedDevice.value)
  await audioRecorder.startRecording(selectedDevice.value)
}
const pauseRecording = async () => {
  //暂停录音
  recordingStatus.value = 2
  await audioRecorder.pauseRecording()
}
const continueRecording = async () => {
  //继续录音
  recordingStatus.value = 1
  await audioRecorder.resumeRecording()
}
const pauseRecord = async () => {
  //停止录音
  recordingStatus.value = 0

  const blob = await audioRecorder.stopRecording()
  console.log('录音已暂停:', blob)

  savedLocally(blob)
}
const returnToPreviousLevel = () => {
  router.push({ path: '/recordingPage' })
}

const savedLocally = async (blob) => {
  const buffer = await blob.arrayBuffer()
  hooks.upath.saveData(buffer, 0, `recording_${new Date().getTime()}.wav`)
}
</script>

<style scoped lang="scss">
.clip-audio {
  width: 100%;
  height: 100vh;
  overflow: hidden;
  box-sizing: border-box;
  background-color: #d9e4f9;
  padding: 5px;

  .clip-audio-content {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    border-radius: 10px;
    background-color: #fff;
    padding: 40px;
  }
  .clip-audio-title {
    font-size: 24px;
    font-weight: bold;
    color: #333;
    text-align: center;
  }
  .clip-audio-wave {
    margin-top: 20px;
    width: 100%;
    height: 200px;
    background-color: #d9e4f9;
  }
  .clip-audio-frequency {
    margin-top: 20px;
    font-size: 16px;
    height: 100px;
    background-color: #d9e4f9;
  }
  .clip-audio-hr {
    margin: 20px 0px 50px;
    position: relative;
    left: 50%;
    width: calc(100% + 80px);
    transform: translateX(-50%);
    height: 1px;
    background-color: #d9e4f9;
  }
  .clip-audio-btn {
    margin-top: 20px;
    display: flex;
    justify-content: center;
    background-color: #d9e4f9;
    gap: 20px;
    padding: 20px 0;
    background-color: #d9e4f9;
  }
  .clip-audio-btn-item {
    padding: 10px 20px;
    border-radius: 5px;
    background-color: #4a90e2;
    color: #fff;
    font-size: 16px;
    font-weight: bold;
    cursor: pointer;
    transition: background-color 0.2s ease;
  }
  .clip-audio-btn-item:hover {
    background-color: #357abd;
  }
}
</style>
